package chatsystem;

import gui.ChatGUI;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import modelcontroller.ChatController;
import modelcontroller.ChatModel;
import network.ChatNI;

/**
 *
 * @author Mariam Kadoura (4IR-RT Group A1)
 * @version 1.1
 *
 */
public class ChatSystem {

    private ChatGUI gui;
    private ChatController controller;
    private ChatModel model;
    private ChatNI ni;

    private ChatSystem(int portMsg, int portFile, int portMsg2, int portFile2) {
        this.model = new ChatModel();
        this.gui = new ChatGUI(model);
        this.ni = new ChatNI(model, portMsg, portFile, portMsg2, portFile2);

        this.controller = new ChatController(model, gui, ni);
        this.gui.setController(controller);
        this.ni.setController(controller);
        model.addObserver(ni);
        model.addObserver(gui);
    }

    /**
     * Main method of the application.
     * <p>
     * 1 argument is needed : choose between "local" or "remote"
     * <p><ul>
     * <li>"local" will create two instances of the Chat System so that the
     * application could be tested on the same machine and
     * <li>"remote" will create one instance of the Chat System, other users
     * with compatible applications have to be connected on the same LAN
     * <ul><p>
     * Ports used: 21000 in UDP and 22000 in TCP
     * <p>
     * To launch the application from the command line: "java -jar
     * ChatSystem.jar"
     *
     * @param args arguments from the command line
     */
    public static void main(String[] args) {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
        }


        if (args.length != 1) {
            System.err.println("The number of arguments is not correct. \nEnter \"local\" is you want to test the application"
                    + " with 2 users on the same computer or \"remote\" if you can test it on different machines");
        } else if ("local".equals(args[0])) {
            ChatSystem cs = new ChatSystem(21000, 22000, 21001, 22001);
            ChatSystem cs2 = new ChatSystem(21001, 22001, 21000, 22000);
        } else if ("remote".equals(args[0])) {
            ChatSystem cs = new ChatSystem(21000, 22000, 21001, 22001);
        } else {
            System.err.println("Wrong argument");
        }
    }
}
